<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="环保登记编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['registrationCode']" placeholder="请输入环保登记编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="车辆识别代码(VIN)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['vin', validatorRules.vin]" placeholder="请输入车辆识别代码(VIN)"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="生产日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择生产日期" v-decorator="['producedTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="车牌颜色" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['plateColor', validatorRules.plateColor]"
                :trigger-change="true" dictCode="cpys" placeholder="请选择车牌颜色" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="车牌号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['plateNo', validatorRules.plateNo]" placeholder="请输入车牌号码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="注册登记日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择注册登记日期" v-decorator="['registerDate']" :trigger-change="true"
                style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="车辆品牌型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['carBrandModel']" placeholder="请输入车辆品牌型号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="燃料类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['fuelType']" :trigger-change="true" dictCode="fuel_type"
                placeholder="请选择燃料类型" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="排放标准" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['evnType', validatorRules.evnType]" :trigger-change="true"
                dictCode="pflx" placeholder="请选择排放标准" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="联网状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['networkingStatus']" :trigger-change="true"
                dictCode="networking_status" placeholder="请选择联网状态" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="随车清单" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple v-decorator="['inventory', validatorRules.inventory]"></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="行驶证" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple v-decorator="['lisense', validatorRules.lisense]"></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="车辆所有人(单位)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['vehicleOwner']" placeholder="请输入车辆所有人(单位)"></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'CarDataForm',
  components: {
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => { },
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      locatin: '',
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
      },
      url: {
        add: "/carData/carData/add",
        edit: "/carData/carData/edit",
        queryById: "/carData/carData/queryById",
        getCompanyName: "/enterpriseBasicInformation/enterpriseBasicInformation/getEnterpriseName",
      }
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    }
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData();
    this.getCompanyName();
  },
  methods: {
    // 获取企业名称
    getCompanyName() {
      getAction(this.url.getCompanyName).then((res) => {
        this.locatin = res.result;
      }).catch((error) => {
        console.log(error);
      })
    },
    add() {
      this.edit({});
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'companyName', 'vin', 'fleet', 'goods', 'lisense', 'inventory', 'status', 'evnType', 'plateColor', 'idCard', 'registerDate', 'carType', 'driverName', 'engineNum', 'plateNo', 'phoneNum', 'statusLedger', 'freightVolume', 'mechanicalModel', 'engineModel', 'pin', 'function', 'fuelType', 'carBrandModel', 'vehicleOwner', 'carSort', 'mechineNameplate', 'producedTime', 'engineProducer', 'networkingStatus', 'registrationCode', 'machineLabel', 'engineNameplate', 'transportGoodsName'))
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId };
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result);
          }
        });
      }
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if (!this.model.id) {
            httpurl += this.url.add;
            method = 'post';
          } else {
            httpurl += this.url.edit;
            method = 'put';
          }
          let formData = Object.assign(this.model, values);
          if (!this.model.carSort) {
            formData.carSort = 1;
          }
          if (!this.model.companyName) {
            formData.companyName = this.locatin;
          }
          console.log("表单提交数据", formData)
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message);
              that.$emit('ok');
            } else {
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, 'companyName', 'vin', 'fleet', 'goods', 'lisense', 'inventory', 'status', 'evnType', 'plateColor', 'idCard', 'registerDate', 'carType', 'driverName', 'engineNum', 'plateNo', 'phoneNum', 'statusLedger', 'freightVolume', 'mechanicalModel', 'engineModel', 'pin', 'function', 'fuelType', 'carBrandModel', 'vehicleOwner', 'carSort', 'mechineNameplate', 'producedTime', 'engineProducer', 'networkingStatus', 'registrationCode', 'machineLabel', 'engineNameplate', 'transportGoodsName'))
    },
  }
}
</script>